package com.zrrd.thread._08_pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author WanJl
 * @version 1.0
 * @title MyThreadPoolDemo
 * @description
 * @create 2023/10/29 16:41
 */
public class MyThreadPoolDemo01 {
    public static void main(String[] args) {
        /*
        static ExecutorService newCachedThreadPool()   创建一个默认的线程池
        static newFixedThreadPool(int nThreads)	    创建一个指定最多线程数量的线程池
         */
        //1、创建一个默认的线程池对象，池子中默认是空的(corePoolSize=0)，默认组多可以容纳int类型的最大值
        ExecutorService executorService = Executors.newCachedThreadPool();
        /*
            Executors类是帮我们创建线程池对象的类。
            ExecutorService类是帮我们控制线程池的类。
         */
        //submit()方法，从线程池中获取线程的方法。
        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });

        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });

        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });

        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });

        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });

        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });

        executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "在执行...");
        });


        //把线程还给线程池的方法
        executorService.shutdown();


    }
}
